package com.icetech.eom.aop;

import com.icetech.eom.common.annotation.SystemLog;
import com.icetech.eom.domain.sys.User;
import com.icetech.eom.domain.tx.Log;
import com.icetech.eom.service.tx.ILogService;
import com.icetech.eom.service.utils.SecurityUtil;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Objects;

/**
 * @Auther: lisc
 * @Date: 2018/11/14 14:41
 * @Description:
 */
@Aspect
@Component
public class SysLogAspect {
    @Autowired
    private ILogService logService;
    @Autowired
    private SecurityUtil securityUtil;
    @Pointcut("@annotation(com.icetech.eom.common.annotation.SystemLog)")
    public void logPointCut() {

    }

    @AfterReturning(returning = "ret", pointcut= "logPointCut()")
    public void around(JoinPoint joinPoint, Object ret) {
        //保存日志
        saveSysLog(joinPoint, ret);
    }

    private void saveSysLog(JoinPoint joinPoint,Object ret) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        Log log = new Log();
        SystemLog methodAnnotation = method.getAnnotation(SystemLog.class);
        if(methodAnnotation != null){
            //注解上的描述
            User currUser = null;
            try {
                currUser = securityUtil.getCurrUser();
            }catch (Exception e){

            }
            if (Objects.isNull(currUser)){
                log.setUserName("admin");
            }else {
                log.setUserName(currUser.getUserName());
            }
            log.setFunName(methodAnnotation.description());
            log.setAction(methodAnnotation.description());
        }

        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("访问方法->"+className + "." + methodName);

        //获取request
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //设置IP地址
        stringBuilder.append("ip地址->"+ getIpAddr(request));
        log.setKeyWord(stringBuilder.toString());
        logService.save(log);
    }

    private String getIpAddr(HttpServletRequest request) {
        String ip = null;
        try {
            ip = request.getHeader("x-forwarded-for");
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } catch (Exception e) {

        }
        return ip;
    }
}
